package de.tabuma.pm.project.controller;

import de.tabuma.pm.controller.*;
import de.tabuma.pm.project.entity.Project;
import de.tabuma.pm.project.logik.ProjectFacade;
import de.tabuma.pm.*;
import de.tabuma.customer.entity.*;

import de.tabuma.lib.*;
import java.io.*;
import java.util.*;
import java.util.logging.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.google.appengine.api.users.*;
import de.tabuma.customer.logik.CustomerFacade;


/**
 *
 * @author alefen
 */
public class create extends tabumacontroller {

    private static final Logger log = Logger.getLogger(create.class.getName());

    public create(String servlet, Hashtable parameter) {
        super(servlet, parameter);
    }

    @Override
    public void navigate(ServletContext context, HttpServletRequest request, HttpServletResponse response, PrintWriter out) {

        UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();

        List<Customer> cList = CustomerFacade.getCustomer(user.getEmail());
        request.setAttribute(constants.paramAction, ""+constants.ACTION_CREATE);
        request.setAttribute(constants.customerList, cList);

        if(request.getParameter(constants.paramProjectCreate)!=null) {            
            
            String projectName = (String) request.getParameter(constants.paramProjectName);
            String projectDescription = (String) request.getParameter(constants.paramProjectDescription);
            String projectCustomerId = (String) request.getParameter(constants.paramProjectCustomerId);
            int projectCustomerIdInt = Integer.valueOf(projectCustomerId).intValue();
            Project project = new Project();
            project.setName(projectName);
            project.setDescription(projectDescription);
            project.setCustomerId(projectCustomerIdInt);
            ProjectFacade.create(project, user.getEmail());

            request.setAttribute(constants.paramAction, "" + constants.ACTION_LIST);

            list listProjectController = new list(this.getServlet(), this.getParameter());
            listProjectController.navigate(context, request, response, out);

        }
    }
    
}